﻿
// **************************************************************************
//    Class Created by Mick Doherty (Dotnetrix) March 2010
//    http://dotnetrix.co.uk/
//
//    ...for GlassUI (an AeroGlass UI Library)
//    http://glassui.codeplex.com/
//    
//    ...modified by element109 July 2010
// **************************************************************************

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using GlassUI.Design;

namespace GlassUI
{
    [Designer(typeof(Design.AutoSizeControlDesigner))]
    [ToolboxBitmap(typeof(Label))]
    public class GlassLabel : GlassAutoSizeControlBase
    {

        public GlassLabel()
            : base()
        {
            base.AutoSize = true;
        }

        #region Private Instance Variables 

        private Image image;

        private ContentAlignment imageAlign = ContentAlignment.MiddleCenter;
        private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
        private TextImageRelation textImageRelation = TextImageRelation.Overlay;

        private ImageList imageList;
        private int imageIndex = -1;
        private string imageKey = "";
        
        #endregion

        #region Properties

        [GlassUIDescription("ButtonImageDescr", "System.Windows.Forms")]
        public Image Image
        {
            get { return image; }
            set 
            {
                if (value != image)
                {
                    image = value;
                    if (value != null)
                        this.ImageList = null;
                    if (this.AutoSize)
                        this.OnSizeChanged(EventArgs.Empty);
                    this.Invalidate();
                }
            }
        }

        [Category("Appearance"), DefaultValue(typeof(ImageList), null)]
        [GlassUIDescription("ButtonImageListDescr", "System.Windows.Forms")]
        public ImageList ImageList
        {
            get { return imageList; }
            set
            {
                imageList = value;
                if (value != null)
                    this.Image = null;
                this.Invalidate();
            }
        }

        [Category("Appearance"), DefaultValue(-1)]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design", typeof(UITypeEditor))]
        [GlassUIDescription("ButtonImageIndexDescr", "System.Windows.Forms")]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int ImageIndex
        {
            get 
            {
                if (this.imageList == null)
                    return -1;
                if (this.imageList.Images.Count < imageIndex)
                    return -1;
                return imageIndex; 
            }
            set
            {
                imageIndex = value;
                if (imageKey.Length > 0)
                    imageKey = "";
                if (this.AutoSize)
                    this.OnSizeChanged(EventArgs.Empty);
                this.Invalidate();
            }
        }

        [DefaultValue("")]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design", typeof(UITypeEditor))]
        [GlassUIDescription("ButtonImageIndexDescr", "System.Windows.Forms")]
        [RefreshProperties(RefreshProperties.All)]
        [TypeConverter(typeof(ImageKeyConverter))]
        public string ImageKey
        {
            get 
            {
                if (this.imageList == null)
                    return "";
                if (!this.imageList.Images.ContainsKey(imageKey))
                    return "";
                return imageKey; 
            }
            set
            {
                imageKey = value;
                if (imageIndex != -1)
                    imageIndex = -1;
                if (this.AutoSize)
                    this.OnSizeChanged(EventArgs.Empty);
                this.Invalidate();
            }
        }

        [Category("Appearance"), DefaultValue(typeof(ContentAlignment), "MiddleCenter")]
        [GlassUIDescription("ButtonImageAlignDescr", "System.Windows.Forms")]
        public ContentAlignment ImageAlign
        {
            get { return imageAlign; }
            set
            {
                if (!Enum.IsDefined(typeof(ContentAlignment), value))
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(ContentAlignment));
                if (imageAlign == value)
                    return;
                imageAlign = value;
                this.Invalidate();
            }
        }

        [Category("Appearance"), DefaultValue(typeof(ContentAlignment), "MiddleCenter")]
        [GlassUIDescription("ButtonTextAlignDescr", "System.Windows.Forms")]
        public ContentAlignment TextAlign
        {
            get { return textAlign; }
            set
            {
                if (!Enum.IsDefined(typeof(ContentAlignment), value))
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(ContentAlignment));
                if (textAlign == value)
                    return;
                textAlign = value;
                this.Invalidate();
            }
        }

        [Category("Behavior"), DefaultValue(typeof(TextImageRelation), "Overlay")]
        [GlassUIDescription("ButtonTextImageRelationDescr", "System.Windows.Forms")]
        public TextImageRelation TextImageRelation
        {
            get { return textImageRelation; }
            set
            {
                if (textImageRelation != value)
                {
                    if (Enum.IsDefined(typeof(TextImageRelation), value))
                    {
                        textImageRelation = value;
                        if (this.AutoSize)
                            this.OnSizeChanged(EventArgs.Empty);
                        this.Invalidate();
                    }
                    else
                    {
                        throw new InvalidEnumArgumentException("value", (int)value, typeof(TextImageRelation));
                    }
                }
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new Color GlowColor
        {
            get{return base.GlowColor;}
            set{base.GlowColor = value;}
        }

        #endregion

        #region Overridden Methods

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);

            //if (this.BackColor.A < 255)
            //{
            //    GlassForm form = this.FindForm() as GlassForm;
            //    bool aeroGlassInUse = !this.DesignMode && form == null ? false : form.ControlIsOnGlass(this);

            //    // Blend into background to make sure we keep Transparency.
            //    if (aeroGlassInUse)
            //    {
            //        pevent.Graphics.CompositingMode = CompositingMode.SourceCopy;
            //        pevent.Graphics.Clear(Color.Empty);
            //    }
            //    else
            //    {
            //        base.DrawParentBackground(pevent.Graphics);
            //    }
            //}
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Image img = this.Image;
            if (this.ImageList != null)
            {
                if (this.imageIndex != -1)
                    img = this.ImageList.Images[this.imageIndex];
                if (this.imageKey.Length > 0)
                    img = this.imageList.Images[this.imageKey];
            }

            GlassUtilities.RenderStandardContent(e.Graphics, this.ClientRectangle, this.Text, this.Font, img, this.TextAlign, this.ImageAlign, this.TextImageRelation, this.ForeColor, true, 8);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (this.AutoSize)
            {
                Size sz = this.GetPreferredSize(this.Size);
                if (this.AutoSizeMode == AutoSizeMode.GrowOnly)
                {
                    sz = new Size(Math.Max(this.Size.Width, sz.Width), Math.Max(this.Size.Height, sz.Height));
                }
                this.Size = sz;
            }
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            this.OnSizeChanged(EventArgs.Empty);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            this.OnSizeChanged(EventArgs.Empty);
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            Size imageSize = Size.Empty;
            Size availableTextSize = Size.Empty;
            Size textSize = Size.Empty;

            Rectangle contentBounds = Rectangle.Empty;
            TextFormatFlags format = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;

            Image img = this.Image;
            if (this.ImageList != null)
            {
                if (this.imageIndex != -1)
                    img = this.ImageList.Images[this.imageIndex];
                if (this.imageKey.Length > 0)
                    img = this.imageList.Images[this.imageKey];
            }

            if (img != null)
                imageSize = img.Size;

            switch (this.TextImageRelation)
            {
                case TextImageRelation.ImageAboveText:
                case TextImageRelation.TextAboveImage:
                    availableTextSize = new Size(proposedSize.Width, proposedSize.Height - imageSize.Height);
                    break;

                case TextImageRelation.ImageBeforeText:
                case TextImageRelation.TextBeforeImage:
                    availableTextSize = new Size(proposedSize.Width - imageSize.Width, proposedSize.Height);
                    break;

                default:
                    availableTextSize = proposedSize;
                    break;
            }

            if (this.Text.Length > 0)
            {
                using (Graphics graphics = this.CreateGraphics())
                {
                    textSize = GlassUtilities.MeasureThemedText(graphics, this.Text, this.Font, availableTextSize, format, 8);
                }
            }

            switch (this.TextImageRelation)
            {
                case TextImageRelation.ImageAboveText:
                case TextImageRelation.TextAboveImage:
                    contentBounds.Width = Math.Max(imageSize.Width, textSize.Width);
                    contentBounds.Height = imageSize.Height + textSize.Height;
                    break;

                case TextImageRelation.ImageBeforeText:
                case TextImageRelation.TextBeforeImage:
                    contentBounds.Width = imageSize.Width + textSize.Width;
                    contentBounds.Height = Math.Max(imageSize.Height, textSize.Height);
                    break;

                default:
                    contentBounds.Width = Math.Max(imageSize.Width, textSize.Width);
                    contentBounds.Height = Math.Max(imageSize.Height, textSize.Height);
                    break;
            }

            contentBounds.Width += Padding.Horizontal;
            contentBounds.Height += Padding.Vertical;

            return contentBounds.Size;
        }

        #endregion

    }
}
